// lib.rs

use napi_derive_ohos::napi;
use std::sync::Mutex;
use once_cell::sync::Lazy;

/// 定义测试用例结构体
#[derive(Clone)]
pub struct AppTest {
    pub name: String,
    pub passed: bool,
}

/// 静态变量存储测试用例列表，方便多次调用
static TESTS: Lazy<Mutex<Vec<AppTest>>> = Lazy::new(|| {
    Mutex::new(vec![
        AppTest { name: "Login Test".to_string(), passed: true },
        AppTest { name: "Checkout Test".to_string(), passed: false },
    ])
});

/// 通过接口添加测试用例
#[napi]
pub fn add_test(name: String, passed: bool) {
    let mut tests = TESTS.lock().unwrap();
    tests.push(AppTest { name, passed });
}

/// 执行所有测试用例，返回失败的测试用例数量（u32，避免使用 u64）
#[napi]
pub fn run_all_tests() -> u32 {
    let tests = TESTS.lock().unwrap();
    let mut failed_count: u32 = 0;

    for test in tests.iter() {
        println!("Running test: {}", test.name);
        if test.passed {
            println!("Test passed!");
        } else {
            println!("Test failed!");
            failed_count += 1;
        }
    }
    failed_count
}

/// 获取测试用例数量
#[napi]
pub fn get_test_count() -> u32 {
    let tests = TESTS.lock().unwrap();
    tests.len() as u32
}

/// 清空测试用例列表
#[napi]
pub fn clear_tests() {
    let mut tests = TESTS.lock().unwrap();
    tests.clear();
}

